package com.itheima.controller.store;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.QuestionItem;
import com.itheima.utils.BeanUtil;
import com.itheima.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// uri:/store/questionItem?operation=list
@WebServlet("/store/questionItem/*")
public class QuestionItemServlet extends BaseServlet {


    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取questionId
        String questionId = request.getParameter("questionId");
        //设置到请求域
        request.setAttribute("questionId", questionId);
        //获取选项列表 无需分页
        PageInfo itemList = questionItemService.findAll(questionId, 1, 100);
        //设置到请求域
        request.setAttribute("page", itemList);
        //跳转页面
        request.getRequestDispatcher("/pages/store/questionItem/list.jsp").forward(request, response);
    }

    //数据回显
    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取选项id
        String id = request.getParameter("id");
        //获取选项对象
        QuestionItem questionItem = questionItemService.findById(id);
        //设置到请求域
        request.setAttribute("questionItem", questionItem);
        //跳转页面
        list(request, response);
    }

    //保存或者更新
    private void saveOrUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取questionId和itemId
        //String questionId = request.getParameter("questionId");
        String itemId = request.getParameter("itemId");
        //封装
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class);
        //判断itemId是否为空来决定是保存还是更新
        if(StringUtils.isNotBlank(itemId)){
            //非空 更新操作
            questionItemService.update(questionItem);
        }else {
            //为空 添加操作
            questionItemService.save(questionItem);
        }
        //完成操作 跳转
        list(request,response);
    }

    private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //将数据获取到，封装成一个对象
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class);
        //调用业务层接口save
        questionItemService.save(questionItem);
        //跳转回到页面list
        list(request, response);
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //将数据获取到，封装成一个对象
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class);
        System.out.println(questionItem);
        //调用业务层接口save
        questionItemService.update(questionItem);
        //跳转回到页面list
        list(request, response);
    }


    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //将数据获取到，封装成一个对象
        QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class);
        //调用业务层接口save
        questionItemService.delete(questionItem);
        //跳转回到页面list
        list(request, response);
    }


    private void list1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String questionId = request.getParameter("questionId");
        //给选项设置页面设置id
        request.setAttribute("questionId", questionId);
        //获取选项数据
        PageInfo page = questionItemService.findAll(questionId, 1, 100);
        //设置请求域数据
        request.setAttribute("page", page);
        //如果操作值没有设置  那就设置保存
        if (request.getAttribute("operation") == null) {
            request.setAttribute("operation", "save");
        }
        //跳转
        request.getRequestDispatcher("/pages/store/questionItem/list.jsp").forward(request, response);
    }

    private void toEdit1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取questionId
        String questionId = request.getParameter("id");
        //获取对象
        QuestionItem questionItem = questionItemService.findById(questionId);
        //设置请求域的值
        request.setAttribute("questionItem", questionItem);
        //设置编辑模式
        request.setAttribute("operation", "edit");
        //跳转
        list(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

}
